package com.InterestingExercises;

import java.util.Random;
import java.util.Scanner;

public class 双色球 {
    public static void main(String[] args) {
        System.out.println("------------------------");
        int[] userNumbers = userSelectNumbers();
//        System.out.println(Arrays.toString(arr));
        System.out.println("用户选择的号码如下:");
        printArr(userNumbers);
        System.out.println("------------------------");
        int[] luckyNumbers = generateLuckyNumbers();
        System.out.println("幸运号码如下:");
        printArr(luckyNumbers);
        areYouLuckyEnough(userNumbers, luckyNumbers);
    }


    //用户选择号码

    /**
     * 用户先选6个1-33的红球,再选一个1-16的蓝球
     *
     * @return int[]
     */
    public static int[] userSelectNumbers() {
        int[] arr = new int[7];
        Scanner sc = new Scanner(System.in);

        //先输入6个红球
        boolean flag;
        int i;
        for (i = 0; i < arr.length - 1; i++) {
            flag = true;
            while (flag) {
                System.out.print("请输入第" + (i + 1) + "个红球号码(1-33之间,不能重复):");
                arr[i] = sc.nextInt();
                System.out.println("------------------------");

                //输入范围判断
                if (arr[i] < 1 || arr[i] > 33) {
                    System.out.println("输入超出范围,请重输!");
                    System.out.println("------------------------");
                    continue;
                }

                //输入查重
                if (isExist(arr, arr[i], i - 1)) {
                    System.out.println("输入重复,请重输!");
                    System.out.println("------------------------");
                } else {
                    flag = false;
                }
            }
        }


        //再输入1个蓝球
        flag = true;
        while (flag) {
            System.out.print("请输入蓝球的号码(1-16之间):");
            arr[i] = sc.nextInt();//输入完红球之后i刚好是arr.length -1
            System.out.println("------------------------");
            //输入范围判断
            if (arr[i] < 1 || arr[i] > 16) {
                System.out.println("输入超出范围,请重输!");
                System.out.println("------------------------");
                continue;
            }
            flag = false;
        }
        return arr;
    }

    /**
     * 打印int[]内容
     *
     * @param arr
     */
    public static void printArr(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if (i < arr.length - 2) {
                System.out.print(arr[i] + ", ");
            } else if (i == arr.length - 2) {
                System.out.print(arr[i] + " | ");
            } else {
                System.out.print(arr[i]);
            }
        }
        System.out.println("]");
    }

    /**
     * 判断数组中endIndex之前的下标是否出现过target,出现了返回true,否则返回false
     *
     * @param arr
     * @param target
     * @param endIndex
     * @return boolean
     */
    public static boolean isExist(int[] arr, int target, int endIndex) {
        for (int i = 0; i <= endIndex; i++) {
            if (arr[i] == target) {
                return true;
            }
        }
        return false;
    }


    //生成幸运号码

    /**
     * 生成幸运号码,返回int[]
     *
     * @return
     */
    public static int[] generateLuckyNumbers() {
        int[] arr = new int[7];
        Random rd = new Random();
        //先随机生成6和个红球
        int i;
        for (i = 0; i < arr.length - 1; i++) {
            boolean flag = true;
            while (flag) {
                arr[i] = rd.nextInt(33) + 1;//随机产生1-33之间的数
                if (isExist(arr, arr[i], i - 1)) {
                    //表示已经出现过了,需要重新随机一个数
                    //do nothing
                } else {
                    flag = false;
                }
            }
        }
        //再随机生成一个蓝球
        arr[i] = rd.nextInt(16) + 1; //i刚好是arr.length - 1
        return arr;
    }


    //判断是否中奖

    /**
     * 判断用户是否足够幸运,输入用户号码int[]和幸运号码int[],无返回值
     *
     * @param userNumbers
     * @param luckyNumbers
     */
    public static void areYouLuckyEnough(int[] userNumbers, int[] luckyNumbers) {
        int redCount = 0, blueCount = 0;
        //判断红球
        for (int i = 0; i < userNumbers.length - 1; i++) {
            for (int j = 0; j < luckyNumbers.length - 1; j++) {
                if (userNumbers[i] == luckyNumbers[j]) {
                    redCount++;
                    System.out.println("红球 " + userNumbers[i] + " 命中了!");
                    break;
                }
            }
        }
        //判断蓝球
        if (userNumbers[userNumbers.length - 1] == luckyNumbers[luckyNumbers.length - 1]) {
            blueCount++;
            System.out.println("蓝球 " + userNumbers[userNumbers.length - 1] + " 命中了!");
        }

        System.out.println("------------------------");
        System.out.println("用户共命中了" + redCount + "个红球,命中了" + blueCount + "个蓝球");
        System.out.println("------------------------");

        //判断具体中奖情况
        if (redCount == 6 && blueCount == 1) {
            System.out.println("恭喜您，中奖1000万，可以开始享受人生了~~~");
        } else if (redCount == 6 && blueCount == 0) {
            System.out.println("恭喜您，中奖500万，可以稍微开始享受人生了~~~");
        } else if (redCount == 5 && blueCount == 1) {
            System.out.println("恭喜您，中奖3000元，可以出去吃顿小龙虾了~");
        } else if (redCount == 5 && blueCount == 0 || redCount == 4 && blueCount == 1) {
            System.out.println("恭喜您，中了小奖：200元~");
        } else if (redCount == 4 && blueCount == 0 || redCount == 3 && blueCount == 1) {
            System.out.println("中了10元~");
        } else if (redCount < 3 && blueCount == 1) {
            System.out.println("中了5元~");
        } else {
            System.out.println("感谢您对福利事业做出的巨大贡献~~~");
        }
    }
}
